﻿/**
 * Created by Administrator on 2017/8/28.
 */
define(['jquery','ContactInfo','ContactAdd','ContactEdit','reg','api'],function ($,ContactInfo,ContactAdd,ContactEdit,reg,api) {
    var ContactList = {
        name: 'contact-list',
        template: ''
        + '<div class="cnntBox addAndEditContactScroll">'
        + '    <div class="cnntVist">'
        + '        <div class="cnntItem clearfix">'
        + '            <div class="connection-title">客户联系信息</div>'
        + '        </div>'
        + '        <!-- 不可编辑-->'
        + '             <contact-info v-bind:relation="customerContact.ContactSelf" v-if="!showConfirmEditBtn" v-bind:pool-type="poolType"></contact-info>'
        + '        <!-- 可编辑-->'
        + '             <contact-edit v-model="customerContactEdit.ContactSelf" v-if="showConfirmEditBtn"></contact-edit>'
        + '        <!--==============================================================-->'
        + '        <div class="cnntItem clearfix">'
        + '            <div class="connection-title">其他联系人信息</div>'
        + '        </div>'
        + '        <!-- 不可编辑-->'
        + '             <contact-info v-for="item in customerContact.ContactRelative" v-bind:relation="item" v-if="!showConfirmEditBtn"  v-bind:pool-type="poolType"></contact-info>'
        + '        <!-- 可编辑-->'
        + '             <contact-edit v-for="item in customerContactEdit.ContactRelative"  v-model="item" v-if="showConfirmEditBtn" ></contact-edit>'
        + '        <!-- 新增联系方式-->'
        + '        <contact-add  v-show="showConfirmAddBtn" v-bind:customerId="customerId"></contact-add>'
        + '        <!-- 底部按钮-->'
        + '        <div class="cnnBtn" v-show="showBtn">'
        + '            <span class="cnn-add-btn" v-on:click="showAdd" v-if="poolType!=\'RecyclePool\'"></span>'
        + '            <span class="cnn-xiugai-btn" v-on:click="showEdit" v-if="poolType!=\'RecyclePool\'"></span>'
        + '        </div>'
        + '        <!--添加 的确认和取消按钮-->'
        + '        <div class="cnnAddBtn addBtn" v-show="showConfirmAddBtn">'
        + '            <div class="confirmBtn" v-on:click="addConfirm()"  v-if="poolType!=\'RecyclePool\'"></div>'
        + '            <div class="concelBtn" v-on:click="cancel()"  v-if="poolType!=\'RecyclePool\'"></div>'
        + '        </div>'
        + '        <!--编辑 的确认和取消按钮-->'
        + '        <div class="cnnAddBtn editBtn" v-show="showConfirmEditBtn">'
        + '            <div class="confirmBtn" v-on:click="editConfirm()"  v-if="poolType!=\'RecyclePool\'"></div>'
        + '            <div class="concelBtn" v-on:click="cancel()"  v-if="poolType!=\'RecyclePool\'"></div>'
        + '        </div>'
        + '    </div>'
        + '</div>',
        props: {
            poolType: String
        },
        data: function () {
            return {
                customerId: "",
                customerContact: {},
                customerContactEdit: {
                    ContactSelf: {},
                    ContactRelative:[]
                },
                showBtn: true,
                showConfirmAddBtn: false,
                showConfirmEditBtn: false,
            };
        },
        components: {
            ContactInfo: ContactInfo,
            ContactAdd: ContactAdd,
            ContactEdit: ContactEdit,
        },
        methods: {
            getInfo: function (Id) {
                var thisList = this;
                api.Get_Customer_All_Contact({customerId: Id})
                    .done(function (data) {
                        if (data.IsSuccess) {
                            thisList.customerContact = data.ResultObject;
                        } else {
                            thisList.$Message.error(data.ErrMsg);
                        }
                    })
                    .fail(function () {
                        thisList.$Message.error('查询出错');
                    })
            },
            showAdd: function () {
                this.showBtn = false;
                this.showConfirmAddBtn = true;
                this.showConfirmEditBtn = false;
            },
            showEdit: function () {
                var thisList = this;
                this.customerContactEdit = {};
                $.extend(true, this.customerContactEdit, this.customerContact);
                this.$nextTick(function () {
                    thisList.showBtn = false;
                    thisList.showConfirmAddBtn = false;
                    thisList.showConfirmEditBtn = true;
                })
            },
            cancel: function () {
                this.showBtn = true;
                this.showConfirmAddBtn = false;
                this.showConfirmEditBtn = false;
            },
            addConfirm: function () {
                //触发submitAddContact
                window.bus.$emit("submitAddContact");
            },
            editConfirm: function () {
                //触发submitEditContact
                //window.bus.$emit("submitEditContact");
                var c = 0;
                for (var s = 0; s < this.customerContactEdit.ContactSelf.Contacts.length; s++) {
                    if (this.customerContactEdit.ContactSelf.Contacts[s].TypeText === "手机") {
                        if (reg.checkPhone(this.customerContactEdit.ContactSelf.Contacts[s].ContactValue)) {
                            this.$Message.error("手机号码错误");
                            return;
                        }
                    }
                }
                for (var i = 0; i < this.customerContactEdit.ContactRelative.length; i++) {
                    if (this.customerContactEdit.ContactRelative[i].Name === "")
                    {
                        c++;
                    }
                    for (var j = 0; j < this.customerContactEdit.ContactRelative[i].Contacts.length; j++) {
                        if (this.customerContactEdit.ContactRelative[i].Contacts[j].TypeText === "手机") {
                            if (reg.checkPhone(this.customerContactEdit.ContactRelative[i].Contacts[j].ContactValue)) {
                                this.$Message.error("手机号码错误");
                                return;
                            }
                        }
                    }
                }
                if (c > 0) {
                    this.$Message.error("姓名不能为空");
                    return;
                }
                this.showBtn = true;
                this.showConfirmAddBtn = false;
                this.showConfirmEditBtn = false;
                var thisList = this;
                api.Post_Edit_Customer_Contact(thisList.customerContactEdit)
                    .done(function (data) {
                        if (data.IsSuccess) {
                            thisList.$Message.success('修改成功');
                            thisList.getInfo(thisList.customerId);
                        } else {
                            thisList.$Message.error(data.ErrMsg);
                        }
                    })
                    .fail(function () {
                        thisList.$Message.error('修改失败');
                    })
            },
            changeContactUsually: function (data) {
                var thisList = this;
                api.Post_Change_Contact_Usually({
                    customerId: thisList.customerId,
                    relationId: data.RelationId,
                    type: data.Type,
                    contactValue: data.ContactValue,
                }).done(function (data) {
                    if (data.IsSuccess) {
                        thisList.$Message.success('更改成功');
                        thisList.getInfo(thisList.customerId);
                    } else {
                        thisList.$Message.error(data.ErrMsg);
                    }
                })
                .fail(function () {
                    thisList.$Message.error('更改失败');
                })
            },
            deleteRelation: function (data) {
                var thisList = this;
                api.Post_Del_Customer_Relative({
                    customerId: thisList.customerId,
                    relationId: data,
                }).done(function (data) {
                    if (data.IsSuccess) {
                        thisList.$Message.success('删除成功');
                        thisList.getInfo(thisList.customerId);
                    } else {
                        thisList.$Message.error(data.ErrMsg);
                    }
                }).fail(function () {
                    thisList.$Message.error('删除失败');
                })
            }
        },
        mounted: function () {
            var thisList = this;
            //监听getCustomerDetail
            window.bus.$on("getCustomerDetail", function (Id) {
                thisList.customerId = Id;
                thisList.getInfo(Id);
            });

            //监听cancelAddOrEdit
            window.bus.$on("cancelAddOrEdit", function () {
                thisList.cancel();
            });

            //监听refreshContact
            window.bus.$on("refreshContact", function () {
                thisList.getInfo(thisList.customerId);
            });

            //监听changeContactUsually
            window.bus.$on("changeContactUsually", function (data) {
                thisList.changeContactUsually(data);
            });

            //监听deleteRelation
            window.bus.$on("deleteRelation", function (data) {
                thisList.deleteRelation(data);
            });
        }
    };
    return ContactList;
});
